<?php 
namespace app\admin\controller;
use app\admin\model\UploadFiles;
/**
* 商品分类管理
*/
class Product extends Base
{
	//商品分类展示
	public function pro_cates(){
		$pc=model("TkProductCate");
		$dataobj=$pc->select();
		if(count($dataobj)==0){
				$this->success("请先添加商品","add_pro_cates");
			}
		foreach ($dataobj as $v) {
			$data[]=$v->toArray();
		}
		$i=0;
		$data=$this->getcates($data);
		foreach ($data as $k=>$v) {
			$data[$k]['pc_name']=str_repeat("&nbsp;&nbsp;--", $v["pc_level"]-1).$v["pc_name"];
		}
		$this->assign("data",$data);
		return $this->fetch();
	}
	//商品分类添加
	public function add_pro_cates(){
		$pc=model("TkProductCate");
		if(request()->isPost()){
			// print_r($_POST);
			$pc_f_id=input("param.pc_f_id");
			if($pc_f_id==0){
				$pc_level=1;	
			}else{
				$d=$pc->find($pc_f_id)->toArray();
				$pc_level=$d['pc_level']+1;
			}	
			$_POST["pc_level"]=$pc_level;
			$num=$pc->allowField(true)->save($_POST);
			if($num){
				$this->success("添加成功",'pro_cates','',1);
			}else{
				$this->error("请重新添加",'pro_cates','',1);
			}
		}else{
			$cateobj=$pc->select();
			if(count($cateobj)==0){
				$cates=[];
			}else{
				// $city=model("TkCity");
				foreach ($cateobj as $key => $value) {
					$cates[]=$value->toArray();
				}
				foreach ($cates as $k=>$v) {
					$cates[$k]['pc_name']=str_repeat("&nbsp;&nbsp;--", $v["pc_level"]-1).$v["pc_name"];
				}
				$cates=$this->getcates($cates);
			}
			$this->assign("cates",$cates);
			return $this->fetch();
		}
	}
	//商品分类删除
	public function del_pro_cates(){
		$pc=model("TkProductCate");
		$pro=model("TkProduct");
		$id=input("param.id");
		$dataobj=$pc->select();
		foreach ($dataobj as $v) {
			$data[]=$v->toArray();
		}
		//print_r($data);
		foreach ($data as $key => $value) {
			if($id==$v['pc_f_id']){
				$this->success("请先删除子类",'pro_cates','',1);		
			}
		}
		$num2=$pro->where("pc_id=$id")->delete();
		$num=$pc->destroy($id);
		//echo $pc->getLastSql();
		if($num){
			$this->success("删除成功",'pro_cates','',1);
		}else{
			$this->error("请重新删除",'pro_cates','',1);
		}
	}
	//商品分类编辑修改
	public function edit_pro_cates(){
		$pc=model("TkProductCate");
		if(request()->isPost()){
			// print_r(input("param."));
			$pc_f_id=input("param.pc_f_id");
			$dataobj=$pc->where("id=$pc_f_id")->select();
			foreach ($dataobj as $key => $value) {
				$data[]=$value->toArray();
			}
			$pc_level=$data[0]['pc_level']+1;
			$_POST['pc_level']=$pc_level;
		 	$num=$pc->allowField(true)->save($_POST,['id'=>input("param.id")]);
			 // echo $pc->getLastSql();
			 // exit;
			if($num){
				$this->success("修改成功",'pro_cates','',1);
			}else{
				$this->error("请重新修改",'','',1);
			}
		}else{
			$id=input("param.id");
			$data=$pc->find($id)->toArray();
			// print_r($data);
			// exit;
			$cateobj=$pc->select();
			foreach ($cateobj as $key => $value) {
				$cates[]=$value->toArray();
			}
			//print_r($cates);
			// exit;
			foreach ($cates as $k=>$v) {
					$cates[$k]['pc_name']=str_repeat("&nbsp;&nbsp;--",$v["pc_level"]-1).$v["pc_name"];
				}
			$this->assign("cates",$cates);
			$this->assign("data",$data);
			return $this->fetch();
		}
	}

	//商品分类状态 即上下架
	public function pro_cates_ifshow(){
		$id=input("param.id");
		$pc=model("TkProductCate");
		$pro=model("TkProduct");
		$dataobj=$pc->select();
		foreach ($dataobj as $v) {
			$data[]=$v->toArray();
		}
		//print_r($data);
		foreach ($data as $key => $value) {
			if($id==$v['pc_f_id']){
				$this->success("请先下架子类",'pro_cates','',1);		
			}
		}
		$ifshow=input("param.ifshow");
		if($ifshow==1){
			$ifshow=0;
		}else{
			$ifshow=1;
		}
		$pc=model("TkProductCate");
		$data=[
			"ifshow"=>$ifshow,
		];
		$num=$pc->where("id=$id")->update($data);
		$num2=$pro->where("pc_id=$id")->update($data);
		// echo $num;
		// echo $pc->getLastSql();
		// exit;
		if($num){
			$this->success("修改成功",'pro_cates','',1);
		}else{
			$this->error("请重新修改",'pro_cates','',1);
		}
	}
	

	//商品管理
	//商品展示
	public function product_list(){
		$pro=model("TkProduct");
		$dataobj=$pro
					->field("id,pname,ifshow,pimgs,price,old_price,sales,pnum,descrip,mktime,die_time")
					->order('id desc')
					->paginate("4",false);
		//exit;
		if(count($dataobj)==0){
			$this->success("请先添加商品","add_product");
		}
		foreach ($dataobj as $key => $value) {
			$data[]=$value->toArray();
		}		
		foreach ($data as $key => $value) {
			$data[$key]['pimgs']=explode(",", $value['pimgs']);
		}
		if(!isset($_GET['page']) || $_GET['page']<1){
			$_GET['page']=1;
			$nowpage=$_GET['page'];

		}else{
			$nowpage=$_GET['page'];
		}
		$nowpage=$_GET['page'];
		$page=$dataobj->render();
		$list=(($_GET['page']-1)*4)+1;
		$this->assign("nowpage",$nowpage);
		$this->assign("list",$list);
		$this->assign("page",$page);
		$this->assign("data",$data);
		return $this->fetch();
	}

	//商品添加
	public function add_product(){
		if(request()->isAjax() && request()->isPost()){
			if($_POST['type']=="province"){
				$province=model("TkProvince");
				$provobj=$province->select();
				foreach ($provobj as $key => $value) {
					$provinces[]=$value->toArray();
				}
				$data='';
				foreach($provinces as $k=>$v){
					if($k==9){
						$selected='selected';
					}else{
						$selected='';
					}
				$data.="<option value='".$v['provinceID']."' ".$selected.">".$v['province']."</option>";
				}
			}elseif($_POST['type']=="city"){
				// if(!isset($_POST['cit'])){
				// 	$cit=0;
				// }else{
				// 	$cit=$_POST['cit'];
				// }
				$city=model("TkCity");
				$id=$_POST['provinceID'];
				$cityobj=$city->where("fatherID=$id")->select();
				foreach ($cityobj as $key => $value) {
					$cities[]=$value->toArray();
				}
				$data='';
				foreach($cities as $k=>$v){
					
						if($k==0){
							$selected='selected';
						}else{
							$selected='';
						}
					$data.="<option value='".$v['cityID']."' ".$selected.">".$v['city']."</option>";
				}
			}
			$result=['status'=>1,'data'=>$data];
			echo json_encode($result);
		}else if(request()->isPost()){
			$files = request()->file('pimgs');
			if(count($files)==0){
				$this->error("select");
			}
			$upload=model("UploadFiles");
			$pro=model("TkProduct");
			$pimgs=$upload->uploadAll($files);
			if(!is_array($pimgs)){
				$inf=$upload->uploadAll($files);
				$this->error("$inf");
			}
			//判断$paths是否是图片 若不是 终止
			$pimgs=implode(",",$pimgs);
			// print_r($paths);
			$_POST['pimgs']=$pimgs;
			//print_r($_POST['pname']);
			// EXIT;
			$pname=$_POST['pname'];
			$total=count($pro->where("pname='{$pname}'")->select());
			if($total==0){
				$num=$pro->allowField(true)->save($_POST);
				if($num==1){
					$this->success("添加成功",'product_list','',1);
				}else{
					$this->error("请重新添加",'add_product','',1);
				}
			}else{
				$this->error("商品已存在",'add_product','',1);
			}
		}else{
			$pc=model("TkProductCate");
			$cateobj=$pc->where("pc_level=2")->select();
			if(count($cateobj)==0){
				$this->error("请先添加二级分类",'add_pro_cates','',1);
			}else{
				// $city=model("TkCity");
				foreach ($cateobj as $key => $value) {
					$cates[]=$value->toArray();
				}
			}
			// print_r($cates);
			// exit;					
			$this->assign("cates",$cates);
			return $this->fetch();
		}
		
	}

	//商品删除
	public function del_product(){
		$pc=model("TkProduct");
		$id=input("param.id");
		$dataobj=$pc->select();
		foreach ($dataobj as $v) {
			$data[]=$v->toArray();
		}
		$page=input("param.page");
		//echo $page;

		$num=$pc->destroy($id);
		//echo $_SERVER["HTTP_REFERER"];
		if($num){ 
			if(count($data)%4==1 && count($data)>4){
				$page=$page-1;
				$this->msg("删除成功",'http://www.think.com/admin/product/product_list.html?page='.$page);
			}else{
				$this->msg("删除成功",'http://www.think.com/admin/product/product_list.html?page='.$page);
			}
		}else{
			$this->msg("删除失败",'http://www.think.com/admin/product/product_list.html?page='.$page);
		}
	}

	//商品修改
	public function  edit_product(){
		$pro=model("TkProduct");
		$id=input("param.id");
		$data=$pro->find($id)->toArray();
		// print_r($data);
		// exit;
		$pimgs=explode(",",$data['pimgs']);
		if(request()->isPost() && request()->isAjax()){

			if($_POST['type']=="province"){
				$provc=$_POST['provc'];
				$province=model("TkProvince");
				$provobj=$province->select();
				foreach ($provobj as $key => $value) {
					$provinces[]=$value->toArray();
				}
				$data='';
				foreach($provinces as $k=>$v){
					if($v['provinceID']==$provc){
						$selected='selected';
					}else{
						$selected='';
					}
				$data.="<option value='".$v['provinceID']."' ".$selected.">".$v['province']."</option>";
				}
				$result=['status'=>1,'data'=>$data];
				echo json_encode($result);

			}elseif($_POST['type']=="city"){
				if(!isset($_POST['cit'])){
					$city=0;
				}else{
					$cit=$_POST['cit'];
				}
			
				$city=model("TkCity");
				$id=$_POST['provinceID'];
				$cityobj=$city->where("fatherID=$id")->select();
				foreach ($cityobj as $key => $value) {
					$cities[]=$value->toArray();
				}
				
				$data='';
				foreach($cities as $k=>$v){
					if(!isset($_POST['cit'])){
						if($k==$cit){
							$selected='selected';
						}else{
							$selected='';
						}
					}else{
						if($v['cityID']==$cit){
							$selected='selected';
						}else{
							$selected='';
						}
					}
					
					$data.="<option value='".$v['cityID']."' ".$selected.">".$v['city']."</option>";
				}
				$result=['status'=>1,'data'=>$data];
				echo json_encode($result);

			}else{
				// print_r($pimgs);
				$src=input("post.src");
				// echo $src;
				$key=array_search($src,$pimgs);
				// echo $key;
				array_splice($pimgs,$key,1);
				// print_r($pimgs);
				$pimgs=implode(",",$pimgs);
				// echo $pimgs;
				$up=[
					'pimgs'=>$pimgs
				];
				$pro->where("id=$id")->update($up);
				exit;
			}
			
			
		}elseif (request()->isPost()) {
			$total = count(request()->file('pimgs'));
			if($total==0){
				$num=$pro->allowField(true)->save(input("param."),["id"=>$id]);
				if($num){
					$this->success("添加成功",'product_list','',1);
				}else{
					$this->error("请重新添加",'edit_product','',1);
				}
			}else{
				$files=request()->file('pimgs');
				$upload=model("UploadFiles");
				$pimgs=$upload->uploadAll($files);
				$inform=$pro->find($id)->toArray();
				// echo $inform['pimgs'];
				$pimgs[]=$inform['pimgs'];
				// print_r($pimgs);
				$pimgs=implode(",",$pimgs);
				$_POST['pimgs']=$pimgs;
				// print_r($_POST);
				$num=$pro->allowField(true)->save($_POST,["id"=>$id]);
				if($num){
					$this->success("添加成功",'product_list','',1);
				}else{
					$this->error("请重新添加",'edit_product','',1);
				}
			}
		}else{
			$pc=model("TkProductCate");
			$cateobj=$pc->where("pc_level=2")->select();
			foreach ($cateobj as $key => $value) {
				$cates[]=$value->toArray();
			}

			$this->assign("cates",$cates);		
			$this->assign("data",$data);		
			$this->assign("pimgs",$pimgs);		
			return $this->fetch();
		}
	}

	//商品上下架
	public function product_ifshow(){
		$ifshow=input("param.ifshow");
		$id=input("param.id");
		if($ifshow==1){
			$ifshow=0;
		}else{
			$ifshow=1;
		}
		$pc=model("TkProduct");
		$data=[
			"ifshow"=>$ifshow,
		];
		$num=$pc->where("id=$id")->update($data);
		//echo $num;
		// echo $pc->getLastSql();
		//exit;
		if($num==1){
			$this->success("修改成功",'product_list','',1);
		}else{
			$this->error("请重新修改",'product_list','',1);
		}
	}
}
?>